﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
using com.vs.DataAccess;
using com.vs.General;

namespace com.vs.DataAccess.DO
{
    public partial class GenericConfigHeaderDO : BaseDO
    {

        #region "Get Model"
        /// <summary>
        /// 将符合数据库主键的持久对象提取到模型对象中，如数据库符合条件的持久对象不存在，则返回空对象
        /// </summary>
        /// <remarks></remarks>
        public static GenericConfigHeaderDO GetModelByPK(Session sess, string stRecordType)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            string stSQL = "SP_GENERICCONFIGHEADER_SelectByPK";
            try
            {
                helper = new DataHelper(sess);
                dtModel = helper.GetTable(CommandType.StoredProcedure, stSQL, new IDataParameter[]{
                    DataHelper.BuildParameter(sess, "@RECORD_TYPE", stRecordType)
                });
                if (!DataManager.IsEmpty(dtModel))
                {
                    return GetModel(dtModel.Rows[0]);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        #endregion

        #region "DataBase Operation"
        /// <summary>
        /// 在数据库里创建该数据访问对象的持久化数据，同时自增长属性OBJECT_ID也会同时得到更新,并提供数据库事务支持
        /// </summary>
        /// <remarks></remarks>
        public static string Insert(Session sess, GenericConfigHeaderDO genCfgHdrDO)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = "SP_GENERICCONFIGHEADER_Insert";
            try
            {
                helper = new DataHelper(sess);
                paras = ValueParas(genCfgHdrDO);
                helper.Insert(CommandType.StoredProcedure, stSQL, paras);
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return "MSG_GEN000";
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public static string Delete(Session sess, GenericConfigHeaderDO genCfgHdrDO, IDbTransaction tran)
        {
            DataHelper helper = null;
            //DataTable dtModel = null;
            string stSQL = "SP_GENERICCONFIGHEADER_DeleteByPK";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, UpdateWherePara(genCfgHdrDO));
                return null;
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return "MSG_GEN000";
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 将该数据访问对象的变化同步到数据库中，如果该数据访问对象在数据库中的持久数据不存在，同步不会发生，同时也不会抛出例外
        /// </summary>
        /// <remarks></remarks>
        public static void Update(Session sess, GenericConfigHeaderDO genCfgHdrDO)
        {
            DataHelper helper = null;
            string stSQL = "SP_GENERICCONFIGHEADER_Update";
            try
            {
                helper = new DataHelper(sess);
                helper.Update(CommandType.StoredProcedure, stSQL, ValueParas(genCfgHdrDO));
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }

        }

        #endregion

        #region "Data Load"
        /// <summary>
        /// 装载对象GENERICCONFIGHEADER的所有持久性对象到集合，如果数据量大，调用此方法会造成性能上的问题，请谨慎使用
        /// </summary>
        /// <remarks></remarks>
        public static DataTable GetAllToTable(Session sess)
        {
            DataHelper helper = null;

            try
            {
                helper = new DataHelper(sess);
                return helper.GetTable(CommandType.Text, SelectSequel);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }
        public static List<GenericConfigHeaderDO> GetAll(Session sess)
        {
            DataTable dtModel = null;

            try
            {
                dtModel = GetAllToTable(sess);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
            }
        }

        /// <summary>
        /// 装载指定列的值等于指定值的的所有持久性GENERICCONFIGHEADER对象到集合
        /// </summary>
        /// <remarks></remarks>
        public static DataTable GetTableByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                return helper.GetTable(CommandType.Text, stSQL, paras);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        public static List<GenericConfigHeaderDO> GetListByColumn(Session sess, string columnName, Object value)
        {
            DataHelper helper = null;
            DataTable dtModel = null;
            IDataParameter[] paras = null;
            string stSQL = null;
            try
            {
                helper = new DataHelper(sess);
                stSQL = SelectSequel + " Where [" + columnName + "] =@Value ";
                paras = new IDataParameter[]{
                    new SqlParameter( "@Value", value)
                };
                dtModel = helper.GetTable(CommandType.Text, stSQL, paras);
                return LoadListFromDataView(dtModel.DefaultView);
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(sess, e);
                return null;
            }
            finally
            {
                if (helper != null)
                    helper.Dispose();
            }
        }

        /// <summary>
        /// 从DataView装载持久性GENERICCONFIGHEADER对象到集合
        /// </summary>
        /// <remarks></remarks>
        private static List<GenericConfigHeaderDO> LoadListFromDataView(DataView dv)
        {
            List<GenericConfigHeaderDO> list = new List<GenericConfigHeaderDO>();
            for (int index = 0; index <= dv.Count - 1; index++)
            {
                list.Add(GetModel(dv[index].Row));
            }
            return list;
        }

        /// <summary>
        /// loading data value  to property from Datarow
        /// </summary>
        /// <remarks></remarks>
        private static GenericConfigHeaderDO GetModel(DataRow row)
        {
            GenericConfigHeaderDO genCfgHdrDO = new GenericConfigHeaderDO();
            if (!row["RECORD_TYPE"].Equals(DBNull.Value)) genCfgHdrDO.RECORD_TYPE = Convert.ToString(row["RECORD_TYPE"]);
            if (!row["OBJECT_ID"].Equals(DBNull.Value)) genCfgHdrDO.OBJECT_ID = Convert.ToInt32(row["OBJECT_ID"]);
            if (!row["DESCRIPTION"].Equals(DBNull.Value)) genCfgHdrDO.DESCRIPTION = Convert.ToString(row["DESCRIPTION"]);
            if (!row["SYSTEM_CREATED"].Equals(DBNull.Value)) genCfgHdrDO.SYSTEM_CREATED = Convert.ToString(row["SYSTEM_CREATED"]);
            if (!row["USER_STAMP"].Equals(DBNull.Value)) genCfgHdrDO.USER_STAMP = Convert.ToString(row["USER_STAMP"]);
            if (!row["FUNCTION_GROUP"].Equals(DBNull.Value)) genCfgHdrDO.FUNCTION_GROUP = Convert.ToString(row["FUNCTION_GROUP"]);
            if (!row["DATE_TIME_STAMP"].Equals(DBNull.Value)) genCfgHdrDO.DATE_TIME_STAMP = Convert.ToDateTime(row["DATE_TIME_STAMP"]);

            return genCfgHdrDO;
        }
        #endregion

        #region "Other function"
        /// <summary>
        /// 该数据访问对象从数据库中提取数据的Sql语句 
        /// </summary>
        /// <remarks></remarks>
        private static string SelectSequel
        {
            get
            {
                return "Select	*  From [GENERIC_CONFIG_HEADER] ";
            }
        }

        /// <summary>
        /// 该数据访问对象的属性值装载到数据库更新参数数组
        /// </summary>
        /// <remarks></remarks>
        private static IDataParameter[] ValueParas(GenericConfigHeaderDO genCfgHdrDO)
        {
            SqlParameter[] paras = new SqlParameter[7];
            paras[0] = new SqlParameter("@RECORD_TYPE", genCfgHdrDO.RECORD_TYPE);
            paras[1] = new SqlParameter("@DESCRIPTION", genCfgHdrDO.DESCRIPTION == String.Empty ? DBNull.Value : (object)genCfgHdrDO.DESCRIPTION);
            paras[2] = new SqlParameter("@SYSTEM_CREATED", genCfgHdrDO.SYSTEM_CREATED == String.Empty ? DBNull.Value : (object)genCfgHdrDO.SYSTEM_CREATED);
            paras[3] = new SqlParameter("@USER_STAMP", genCfgHdrDO.USER_STAMP == String.Empty ? DBNull.Value : (object)genCfgHdrDO.USER_STAMP);
            paras[4] = new SqlParameter("@FUNCTION_GROUP", genCfgHdrDO.FUNCTION_GROUP == String.Empty ? DBNull.Value : (object)genCfgHdrDO.FUNCTION_GROUP);
            paras[5] = new SqlParameter("@DATE_TIME_STAMP", genCfgHdrDO.DATE_TIME_STAMP == DateTime.MinValue ? DBNull.Value : (object)genCfgHdrDO.DATE_TIME_STAMP);
            paras[6] = new SqlParameter("@OBJECT_ID", genCfgHdrDO.OBJECT_ID);

            paras[0].DbType = DbType.String;
            paras[1].DbType = DbType.String;
            paras[2].DbType = DbType.String;
            paras[3].DbType = DbType.String;
            paras[4].DbType = DbType.String;
            paras[5].DbType = DbType.DateTime;
            paras[6].DbType = DbType.Decimal;

            return paras;
        }

        private static IDataParameter[] UpdateWherePara(GenericConfigHeaderDO genCfgHdrDO)
        {
            SqlParameter[] paras = new SqlParameter[1];
            paras[0] = new SqlParameter("@RECORD_TYPE", genCfgHdrDO.RECORD_TYPE);
            return paras;
        }

        #endregion

    }
}
